package com.wu.project.admin.service.impl;

import com.wu.project.admin.component.JsonResult;
import com.wu.project.admin.dao.AdvertisingDao;
import com.wu.project.admin.entity.AdQueryVO;
import com.wu.project.admin.entity.AdvertisingDTO;
import com.wu.project.admin.entity.PageData;
import com.wu.project.admin.service.AdvertisingService;
import com.wu.project.admin.util.CommonResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xuan
 */
@Service
public class AdvertisingServiceImpl implements AdvertisingService {

    @Autowired
    AdvertisingDao advertisingDao;

    @Override
    public JsonResult getAdvertising(AdQueryVO adQueryVO) {
        Integer offset = CommonResultUtil.Instance.getOffset(adQueryVO.getPageNum(), adQueryVO.getPageSize());
        adQueryVO.setOffset(offset);

        List<AdvertisingDTO> advertisingList = advertisingDao.queryAdvertising(adQueryVO);
        Integer total = advertisingDao.queryAdvertisingCount(adQueryVO);
        PageData pageData = new PageData();
        pageData.setPageSize(adQueryVO.getPageSize());
        pageData.setPageNum(adQueryVO.getPageNum());
        pageData.setTotal(total);
        pageData.setData(advertisingList);
        return CommonResultUtil.Instance.success(pageData);
    }

    @Override
    public JsonResult updateAdvertisingState(AdvertisingDTO advertisingDTO) {
        Integer state = advertisingDao.updateState(advertisingDTO);
        if (state > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult addAd(AdQueryVO adQueryVO) {
        return CommonResultUtil.Instance.success(advertisingDao.addAd(adQueryVO));
    }
}
